package com.chinatechstar.file.service.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.chinatechstar.admin.entity.SysUser;
import com.chinatechstar.admin.mapper.SysUserMapper;
import com.chinatechstar.component.commons.utils.CurrentUserUtils;
import com.chinatechstar.file.entity.SysFileBusiness;
import com.chinatechstar.file.mapper.SysFileBusinessMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinatechstar.file.entity.SysFileHistory;
import com.chinatechstar.file.mapper.SysFileHistoryMapper;
import com.chinatechstar.file.service.SysFileHistoryService;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.utils.SequenceGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 业务逻辑实现层
 */
@Service
@Transactional
public class SysFileHistoryServiceImpl implements SysFileHistoryService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private static final SequenceGenerator SEQUENCE_GENERATOR = new SequenceGenerator();

    @Autowired
    private SysFileHistoryMapper sysFileHistoryMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysFileBusinessMapper sysFileBusinessMapper;

    /**
     * 查询所有数据
     */
    @Override
    public List<SysFileHistory> findSysFileHistoryAll() {
        return sysFileHistoryMapper.findSysFileHistoryAll();
    }

    /**
     * 查询分页
     */
    @Override
    public Map<String, Object> findSysFileHistoryByCondition(Integer currentPage, Integer pageSize, String name, String sorter) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        if (StringUtils.isNotBlank(sorter)) {
            String sort = sorter.substring(0, sorter.lastIndexOf('_'));
            String sequence = "ascend".equals(sorter.substring(sorter.lastIndexOf('_') + 1)) ? "ASC" : "DESC";
            paramMap.put("sort", sort);
            paramMap.put("sequence", sequence);
        } else {
            paramMap.put("sort", "createTime");
            paramMap.put("sequence", "DESC");
        }
        Page<Object> page = PageHelper.startPage(currentPage, pageSize);
        List<LinkedHashMap<String, Object>> resultList = sysFileHistoryMapper.findSysFileHistoryByCondition(paramMap);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), currentPage, pageSize);
    }

    /**
     * 查询导出数据列表
     */
    @Override
    public List<LinkedHashMap<String, Object>> findSysFileHistoryForExcel(Map<String, Object> paramMap) {
        return sysFileHistoryMapper.findSysFileHistoryByCondition(paramMap);
    }

    /**
     * 根据主键ID数据查询数据
     */
    @Override
    public SysFileHistory findSysFileHistoryById(Long id) {
        return sysFileHistoryMapper.findSysFileHistoryById(id);
    }

    /**
     * 根据主键ID数组查询数据
     */
    @Override
    public List<SysFileHistory> findSysFileHistoryByIds(Long[] ids) {
        return sysFileHistoryMapper.findSysFileHistoryByIds(ids);
    }

    /**
     * 根据文件业务ID查询数据
     */
    @Override
    public List<SysFileHistory> findSysFileHistoryByFileId(Long id) {
        SysFileBusiness sysFileBusiness = sysFileBusinessMapper.findSysFileBusinessById(id);
        if (sysFileBusiness != null) {
            return sysFileHistoryMapper.findSysFileHistoryByFileId(sysFileBusiness.getFileId());
        }
        return null;
    }

    /**
     * 新增
     */
    @Override
    public void insertSysFileHistory(SysFileHistory sysFileHistory) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        sysFileHistory.setId(SEQUENCE_GENERATOR.nextId());
        sysFileHistory.setCreateName(currUser.getNickname());
        sysFileHistory.setCreateUsername(currUser.getUsername());
        sysFileHistoryMapper.insertSysFileHistory(sysFileHistory);
        logger.info("已新增： {}", sysFileHistory.getId());
    }

    /**
     * 批量新增
     */
    @Override
    public void insertSysFileHistorys(List<SysFileHistory> sysFileHistorys) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        for (SysFileHistory sysFileHistory : sysFileHistorys) {
            sysFileHistory.setId(SEQUENCE_GENERATOR.nextId());
            sysFileHistory.setCreateName(currUser.getNickname());
            sysFileHistory.setCreateUsername(currUser.getUsername());
        }
        sysFileHistoryMapper.insertSysFileHistorys(sysFileHistorys);
    }

    /**
     * 编辑
     */
    @Override
    public void updateSysFileHistory(SysFileHistory sysFileHistory) {
        sysFileHistoryMapper.updateSysFileHistory(sysFileHistory);
        logger.info("已编辑： {}", sysFileHistory.getId());
    }

    /**
     * 批量编辑
     */
    @Override
    public void updateSysFileHistorys(List<SysFileHistory> sysFileHistorys) {
        sysFileHistoryMapper.updateSysFileHistorys(sysFileHistorys);
    }

    /**
     * 删除
     */
    @Override
    public void deleteSysFileHistoryById(Long id) {
        sysFileHistoryMapper.deleteSysFileHistoryById(id);
    }

    /**
     * 批量删除
     */
    @Override
    public void deleteSysFileHistoryByIds(Long[] ids) {
        sysFileHistoryMapper.deleteSysFileHistoryByIds(ids);
    }

    /**
     * 软删除
     */
    @Override
    public void softDeleteSysFileHistoryById(Long id) {
//        SysFileHistory sysFileHistory = sysFileHistoryMapper.findSysFileHistoryById(id);
//        if (sysFileHistory != null) {
//            sysFileHistory.setSoftDelete(0);
//            sysFileHistoryMapper.updateSysFileHistory(sysFileHistory);
//        }

    }

    /**
     * 批量软删除
     */
    @Override
    public void softDeleteSysFileHistoryByIds(Long[] ids) {
//        List<SysFileHistory> sysFileHistorys = sysFileHistoryMapper.findSysFileHistoryByIds(ids);
//
//        if (sysFileHistorys.size() > 0) {
//            for (SysFileHistory sysFileHistory : sysFileHistorys) {
//                sysFileHistory.setSoftDelete(0);
//            }
//
//            sysFileHistoryMapper.updateSysFileHistorys(sysFileHistorys);
//        }
    }

}
